package com.yuexun.dhdoctor.module.Chartered.ui;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.BaseEntity;
import com.yuexun.dhdoctor.base.BaseKJActivity;
import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.module.Chartered.adapter.CharteredAdapter;
import com.yuexun.dhdoctor.module.Chartered.bean.CharteredBean;
import com.yuexun.dhdoctor.module.Chartered.model.impl.CharteredModel;
import com.yuexun.dhdoctor.module.Chartered.presenter.ICharteredPresenter;
import com.yuexun.dhdoctor.module.Chartered.presenter.impl.CharteredPresenter;
import com.yuexun.dhdoctor.module.Chartered.ui.view.ICharteredView;
import com.yuexun.dhdoctor.module.contractmanage.adapter.ContractManageAdapter;
import com.yuexun.dhdoctor.module.contractmanage.model.impl.ContractManageModel;
import com.yuexun.dhdoctor.module.contractmanage.presenter.impl.ContractManagePresenter;
import com.yuexun.dhdoctor.module.contractmanage.ui.Act_ContractManageInspectDetail;
import com.yuexun.dhdoctor.widget.EmptyLayout;
import com.yuexun.dhdoctor.widget.PullToRefresh.FooterLoadingLayout;
import com.yuexun.dhdoctor.widget.PullToRefresh.PullToRefreshBase;
import com.yuexun.dhdoctor.widget.PullToRefresh.PullToRefreshList;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2019/1/29 0029.
 */

public class Act_Chartered extends BaseKJActivity implements ICharteredView {

    @Bind(R.id.filter_edittext)
    EditText filterEdittext;
    @Bind(R.id.search_ll)
    LinearLayout searchLl;
    @Bind(R.id.chartered_list)
    PullToRefreshList charteredList;
    @Bind(R.id.empty_layout)
    EmptyLayout emptyLayout;
    @Bind(R.id.ll_01)
    RelativeLayout ll01;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.tv_sex)
    TextView tvSex;
    @Bind(R.id.tv_tel)
    TextView tvTel;
    @Bind(R.id.tv_mail)
    TextView tvMail;
    @Bind(R.id.tv_nvq)
    TextView tvNvq;
    @Bind(R.id.tv_useful_time)
    TextView tvUsefulTime;
    @Bind(R.id.tv_tcb)
    TextView tvTcb;
    @Bind(R.id.tv_type)
    TextView tvType;
    @Bind(R.id.tv_range)
    TextView tvRange;
    @Bind(R.id.tv_andress)
    TextView tvAndress;
    @Bind(R.id.tv_record_date)
    TextView tvRecordDate;
    @Bind(R.id.tv_booker)
    TextView tvBooker;
    @Bind(R.id.tv_job_number)
    TextView tvJobNumber;
    @Bind(R.id.tv_dept)
    TextView tvDept;
    @Bind(R.id.tv_submit_time)
    TextView tvSubmitTime;
    @Bind(R.id.tv_remark)
    TextView tvRemark;
    @Bind(R.id.ll_02)
    ScrollView ll02;

    private ListView mList;

    CharteredAdapter adapter;

    ICharteredPresenter iCharteredPresenter;

    ArrayList<CharteredBean> charteredBeans = new ArrayList<>();

    @Override
    public void setRootView() {
        setContentView(R.layout.act_chartered);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        listViewPreference();
        iCharteredPresenter = new CharteredPresenter(this, new CharteredModel());
        mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("Chartered", charteredBeans.get(position));
                Intent intent = new Intent(aty, Act_Chartered_Detail.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }


    @OnClick({R.id.back, R.id.patient_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.patient_search:
                if (filterEdittext.getText().toString().length() > 0) {
                    ApiForGetList();
                }
                break;
        }
    }

    /**
     * 初始化ListView样式
     */
    private void listViewPreference() {
        mList = charteredList.getRefreshView();
        mList.setDivider(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
        mList.setOverscrollFooter(null);
        mList.setOverscrollHeader(null);
        mList.setOverScrollMode(ListView.OVER_SCROLL_NEVER);
        charteredList.setPullLoadEnabled(false);//设置上拉刷新功能关闭
        ((FooterLoadingLayout) charteredList.getFooterLoadingLayout()).setNoMoreDataText("已经是最新状态");

        adapter = new CharteredAdapter(mList, charteredBeans, R.layout.item_chartered);
        mList.setAdapter(adapter);

        charteredList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
//                nowpage = 1;
//                ApiForGetList();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
//                nowpage = nowpage + 1;
//                ApiForGetList();
            }
        });

        emptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
//                nowpage = 1;
                ApiForGetList();
            }
        });
    }

    //请求详情
    private void ApiForGetList() {
        Map<String, String> params = new HashMap<>();
        params.put("sessionUuid", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
        params.put("pageIndex", "1");
        params.put("pageSize", "9999");
        params.put("userNameOrIdentityNum", filterEdittext.getText().toString());
        iCharteredPresenter.showCharteredList(params);
    }

    @Override
    public void showCharteredList(BaseEntity<CharteredBean> charteredBeanBaseEntity) {
        if (charteredList.isPullRefreshEnabled()) {
            charteredList.onPullDownRefreshComplete();
        }

        if (charteredBeanBaseEntity.getStatus().equals("200")) {
            if (charteredBeanBaseEntity.getDatas().size() == 0) {
                charteredList.setHasMoreData(false);
            } else {
                if (charteredList.isPullLoadEnabled()) {
                    charteredList.onPullUpRefreshComplete();
                }
            }

            if (charteredBeanBaseEntity.getDatas().size() == 1) {
                ll01.setVisibility(View.GONE);
                ll02.setVisibility(View.VISIBLE);
                tvName.setText(charteredBeanBaseEntity.getDatas().get(0).getUserName());
                if(charteredBeanBaseEntity.getDatas().get(0).getSex() == 1)
                {
                    tvSex.setText("男");
                }
                else if(charteredBeanBaseEntity.getDatas().get(0).getSex() ==2)
                {
                    tvSex.setText("女");
                }
                else {
                    tvSex.setText("不详");
                }
                tvTel.setText(charteredBeanBaseEntity.getDatas().get(0).getPhoneNum());
                tvMail.setText(charteredBeanBaseEntity.getDatas().get(0).getEmail());
                tvNvq.setText(charteredBeanBaseEntity.getDatas().get(0).getLicenseCertificateNum());
                tvUsefulTime.setText(charteredBeanBaseEntity.getDatas().get(0).getBeginTime()+"--"+charteredBeanBaseEntity.getDatas().get(0).getEndTime());
                tvTcb.setText(charteredBeanBaseEntity.getDatas().get(0).getIssuingAgency());
                tvType.setText(charteredBeanBaseEntity.getDatas().get(0).getPracticeType());
                tvRange.setText(charteredBeanBaseEntity.getDatas().get(0).getPracticeScope());
                tvAndress.setText(charteredBeanBaseEntity.getDatas().get(0).getPracticePlace());
                tvRecordDate.setText(charteredBeanBaseEntity.getDatas().get(0).getCreateTime());
                tvBooker.setText(charteredBeanBaseEntity.getDatas().get(0).getAccountName());
                tvJobNumber.setText(charteredBeanBaseEntity.getDatas().get(0).getJobNum());
                tvDept.setText(charteredBeanBaseEntity.getDatas().get(0).getDepartment());
                tvSubmitTime.setText(charteredBeanBaseEntity.getDatas().get(0).getCreateDt());
                tvRemark.setText(charteredBeanBaseEntity.getDatas().get(0).getPracticeDtl());
            } else {
                ll01.setVisibility(View.VISIBLE);
                ll02.setVisibility(View.GONE);
                charteredBeans.clear();
                charteredBeans = charteredBeanBaseEntity.getDatas();
            }
            adapter.refresh(charteredBeans);
        } else {
            ViewInject.toast(charteredBeanBaseEntity.getMessage());
        }

        if (charteredBeanBaseEntity.getDatas().size() == 0) {
            emptyLayout.setErrorType(EmptyLayout.NODATA);
        } else {
            emptyLayout.dismiss();
        }
    }
}
